package com.hnote.hnoteapplication.ui.activity;

import android.graphics.drawable.Drawable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.content.ContextCompat;
import android.widget.RadioGroup;

import com.hnote.hnoteapplication.R;
import com.hnote.hnoteapplication.base.DataBindingActivity;
import com.hnote.hnoteapplication.databinding.ActivityMainBinding;
import com.hnote.hnoteapplication.ui.fragment.HomeFragment;
import com.hnote.hnoteapplication.ui.fragment.MenuFragment;
import com.hnote.hnoteapplication.ui.fragment.MyFragment;


/**
 * 描述：MainActivity
 * 作者：HNote
 * 邮箱：liupengfei930311@163.com
 * 日期：2017-03-07 下午10:21
 **/
public class MainActivity extends DataBindingActivity<ActivityMainBinding> implements RadioGroup.OnCheckedChangeListener {

    // 定义三个导航的fragment，主页 应用 个人
    private Fragment homeFragment, menuFragment, myFragment;
    //当前显示的Fragment
    private Fragment mFragment;

    @Override
    public int getLayoutId() {
        return R.layout.activity_main;
    }

    @Override
    public void initView() {

        // 开启底部radiogroup的check监听
        mViewBinding.navigationBtn.setOnCheckedChangeListener(this);

        // 获取fragment对象
        homeFragment = new HomeFragment();
        menuFragment = new MenuFragment();
        myFragment = new MyFragment();

        // 设置当前呈现的fragment
        FragmentManager fragmentManager = getSupportFragmentManager();
        fragmentManager.beginTransaction().add(R.id.main_fragment, homeFragment).commit();

        mFragment = homeFragment;

        mViewBinding.home.setOnClickListener((v) -> {});

        mViewBinding.home.setOnLongClickListener((v) -> false);
    }

    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {

        Drawable homeDrawable = null, menuDrawable = null, myDrawable = null;

        switch (checkedId) {

            case R.id.home:
                mViewBinding.home.setChecked(true);
                mViewBinding.menu.setChecked(false);
                mViewBinding.my.setChecked(false);

                homeDrawable = ContextCompat.getDrawable(MainActivity.this, R.mipmap.home_n);
                menuDrawable = ContextCompat.getDrawable(MainActivity.this, R.mipmap.menu_p);
                myDrawable = ContextCompat.getDrawable(MainActivity.this, R.mipmap.my_p);

                homeDrawable.setBounds(0, 0, homeDrawable.getMinimumWidth(), homeDrawable.getMinimumHeight());
                menuDrawable.setBounds(0, 0, menuDrawable.getMinimumWidth(), menuDrawable.getMinimumHeight());
                myDrawable.setBounds(0, 0, myDrawable.getMinimumWidth(), myDrawable.getMinimumHeight());

                switchFragment(homeFragment);

                break;

            case R.id.menu:
                mViewBinding.home.setChecked(false);
                mViewBinding.menu.setChecked(true);
                mViewBinding.my.setChecked(false);

                homeDrawable = ContextCompat.getDrawable(MainActivity.this, R.mipmap.home_p);
                menuDrawable = ContextCompat.getDrawable(MainActivity.this, R.mipmap.menu_n);
                myDrawable = ContextCompat.getDrawable(MainActivity.this, R.mipmap.my_p);

                homeDrawable.setBounds(0, 0, homeDrawable.getMinimumWidth(), homeDrawable.getMinimumHeight());
                menuDrawable.setBounds(0, 0, menuDrawable.getMinimumWidth(), menuDrawable.getMinimumHeight());
                myDrawable.setBounds(0, 0, myDrawable.getMinimumWidth(), myDrawable.getMinimumHeight());

                switchFragment(menuFragment);

                break;

            case R.id.my:
                mViewBinding.home.setChecked(false);
                mViewBinding.menu.setChecked(false);
                mViewBinding.my.setChecked(true);

                homeDrawable = ContextCompat.getDrawable(MainActivity.this, R.mipmap.home_p);
                menuDrawable = ContextCompat.getDrawable(MainActivity.this, R.mipmap.menu_p);
                myDrawable = ContextCompat.getDrawable(MainActivity.this, R.mipmap.my_n);

                homeDrawable.setBounds(0, 0, homeDrawable.getMinimumWidth(), homeDrawable.getMinimumHeight());
                menuDrawable.setBounds(0, 0, menuDrawable.getMinimumWidth(), menuDrawable.getMinimumHeight());
                myDrawable.setBounds(0, 0, myDrawable.getMinimumWidth(), myDrawable.getMinimumHeight());

                switchFragment(myFragment);

                break;
        }

        mViewBinding.home.setCompoundDrawables(null, null, null, homeDrawable);
        mViewBinding.menu.setCompoundDrawables(null, null, null, menuDrawable);
        mViewBinding.my.setCompoundDrawables(null, null, null, myDrawable);
    }

    /**
     *  设置fragment切换
     */
    private void switchFragment(Fragment fragment) {
        //判断当前显示的Fragment是不是切换的Fragment
        if (mFragment != fragment) {
            //判断切换的Fragment是否已经添加过
            if (!fragment.isAdded()) {
                //如果没有，则先把当前的Fragment隐藏，把切换的Fragment添加上
                getSupportFragmentManager().beginTransaction().hide(mFragment)
                        .add(R.id.main_fragment, fragment).commit();
            } else {
                //如果已经添加过，则先把当前的Fragment隐藏，把切换的Fragment显示出来
                getSupportFragmentManager().beginTransaction().hide(mFragment).show(fragment).commit();
            }
            mFragment = fragment;
        }
    }
}
